<?php  /* TICKETSMITH $Id: index.php,v 1.23 2005/02/16 06:26:14 ajdonnison Exp $ */

if (!$canAccess) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// setup the title block
$titleBlock = new CTitleBlock( 'Trouble Ticket Management' );
$titleBlock->show();

require("modules/ticketsmith/config.inc.php");
require("modules/ticketsmith/common.inc.php");

$column = $CONFIG["order_by"];
$direction = $CONFIG["message_order"];
$offset = 0;
$limit = $CONFIG["view_rows"];

$type = dPgetParam( $_GET, 'type', '' );
$column = dPgetParam( $_GET, 'column', $column);
$direction = dPgetParam( $_GET, 'direction', $direction);
$offset = dPgetParam( $_GET, 'offset', $offset);
$action = dPgetParam( $_REQUEST, 'action', null );

if($type == ''){
	if($AppUI->getState("ticket_type")){
		$type = $AppUI->getState("ticket_type");
	} else {
		$type = "Open";
	}
} else {
	$AppUI->setState("ticket_type", $_GET["type"]);
}


/* expunge deleted tickets */
if (@$action == "expunge") {
    $deleted_parents = column2array("SELECT ticket FROM tickets WHERE type = 'Deleted'");
    for ($loop = 0; $loop < count($deleted_parents); $loop++) {
        do_query("DELETE FROM tickets WHERE ticket = '$deleted_parents[$loop]'");
        do_query("DELETE FROM tickets WHERE parent = '$deleted_parents[$loop]'");
    }
}

/* setup table & database field stuff */
if ($dPconfig['link_tickets_kludge']) {
	$fields = array("headings" => array("View", "Author", "Subject", "Date",
                                    "Followup", "Status", "Priority", "Owner", "Link"),

                "columns"  => array("ticket", "author", "subject", "timestamp",
                                    "activity", "type", "priority", "assignment", "ticket"),

                "types"    => array("view", "email", "normal", "open_date",
                                    "activity_date", "normal", "priority_view", "user", "attach"),

                "aligns"   => array("center", "left", "left", "left", "left",
                                    "center", "center", "center", "center"));
} else {
	$fields = array("headings" => array("View", "Author", "Subject", "Date",
                                    "Followup", "Status", "Priority", "Owner"),

                "columns"  => array("ticket", "author", "subject", "timestamp",
                                    "activity", "type", "priority", "assignment"),

                "types"    => array("view", "email", "normal", "open_date",
                                    "activity_date", "normal", "priority_view", "user"),

                "aligns"   => array("center", "left", "left", "left", "left",
                                    "center", "center", "center"));
}

/* count tickets */
$query = "SELECT COUNT(*) FROM tickets WHERE parent = '0'";
if ($type != 'All') {
    $query .= " AND type = '$type'";
}
$ticket_count = query2result($query);

/* paging controls */
if (($offset + $limit) < $ticket_count) {
    $page_string = ($offset + 1) . " to " . ($offset + $limit) . " of $ticket_count";
}
else {
    $page_string = ($offset + 1) . " to $ticket_count of $ticket_count";
}

/* form query */
$select_fields= join(', ', $fields['columns']);
$query = "SELECT $select_fields FROM tickets WHERE ";
if ($type == 'My') {
    $query .= "type = 'Open' AND (assignment = '$AppUI->user_id' OR assignment = '0') AND ";
}
elseif ($type != "All") {
    $query .= "type = '$type' AND ";
}
$query .= "parent = '0' ORDER BY " . urlencode($column) . " $direction LIMIT $offset, $limit";

/* do query */
$result = do_query($query);
$parent_count = number_rows($result);

/* start table */
?>

<table id="idTicketList" class="list" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr class="first">
	<td colspan="<?php echo count( $fields["headings"] );?>" align="center">
		<table width="100%" border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td class="internal">
			<?
				$types = array('My','Open','Processing','Closed','Deleted','All');
				$action = "location.href='index.php?m=ticketsmith&type='+this.options[this.selectedIndex].text";
				echo '<b>'.$AppUI->_('Ticket').'</b>: '.
					 arraySelect($types, 'type', 'onchange="'.$action.'"', array_search($type, $types));
			?>
			</td>
			<td width="33%" align="center" valign="middle" class="internal">
				<?php
				if ($ticket_count > $limit) {
				    if ($offset - $limit >= 0) {
				        print("<a href=index.php?m=ticketsmith&type=$type&column=$column&direction=$direction&offset=" . ($offset - $limit) . "><img src=images/navleft.gif border=0></a> | \n");
				    }
				    print($AppUI->_("$page_string")."\n");
				    if ($offset + $limit < $ticket_count) {
				        print(" | <a href=index.php?m=ticketsmith&type=$type&column=$column&direction=$direction&offset=" . ($offset + $limit) . "><img src=images/navright.gif border=0></a>\n");
				    }
				}
				?>
			</td>
			<td class="internal" align="right">
			<?
				/* output action buttons */

				showButton($AppUI->_('Search'), 'index.php?m=ticketsmith&a=search');
				showButton($AppUI->_('Report as PDF'), 'index.php?m=ticketsmith&a=pdf&type='.$type.'&suppressHeaders=1', 'ReportPDF');
				if ($canEdit) {
					showButton($AppUI->_('New Ticket'), 'index.php?m=ticketsmith&a=post_ticket');
				}
				if ($type == 'Deleted' && $parent_count) {
					showButton($AppUI->_('Expunge Deleted'), 'index.php?m=ticketsmith&type=Deleted&action=expunge');
				}
			?>
			</td>
		</tr>
		</table>
	</td>
</tr>

<?php
/* output headings */
echo '<tr>';
for ($n = 0; $n < count($fields['headings']); $n++) {
	if ($column != $fields['columns'][$n])
		$new_direction = 'ASC';
	else
		$new_direction = $direction == 'ASC' ? 'DESC' : 'ASC';
	echo '<th align="' . $fields['aligns'][$n] . '" style="padding: 4px">';
	echo '<a href="index.php?m=ticketsmith&type='.$type.'&column='.$fields['columns'][$n].'&direction='.$new_direction.'">';
	echo $AppUI->_($fields['headings'][$n]) . '</a></th>';
}
echo '</tr>';

/* output tickets */
if ($parent_count) {
	global $CONFIG;

	$nticks = 1;
	while ($row = result2hash($result)) {
		$value = $row['ticket'];
		if ($CONFIG['index_link'] == 'latest') {
			$latest_value = query2result("SELECT ticket FROM tickets WHERE parent = '$value' ORDER BY ticket DESC LIMIT 1");
			if ($latest_value) {
				$value = $latest_value;
			}
		}
		$action = "location.href = 'index.php?m=ticketsmith&a=view&ticket=$value'";

		echo '<tr class="' . (($nticks % 2 == 1) ? 'first' : 'second') . '" onclick="'.$action.'" style="cursor:pointer">';
		echo '<td align="center"><img src="images/view.gif"></td>';
		for ($n = 1; $n < count($fields['headings']); $n++) {
			echo '<td align=' . $fields['aligns'][$n] . '>';

			//translate some information, some not
			if ($fields['headings'][$n] == 'Status') {
				echo $AppUI->_(format_field($row[$fields['columns'][$n]], $fields['types'][$n], $row[$fields['columns'][0]]));
			}
			else {
				echo format_field($row[$fields['columns'][$n]], $fields['types'][$n], $row[$fields['columns'][0]]);
			}
			echo '</td>';
		}
		echo '</tr>';
		$nticks++;
	}

	echo '<script>Table("idTicketList").setNormalHover(2)</script>';
}
else {
	echo '<tr class="second">';
	echo '<td align="center" colspan="100%">';
	echo $AppUI->_('There are no').' ';
	echo $type == 'All' ? '' : strtolower($AppUI->_($type)) . ' ';
	echo $AppUI->_('tickets');
	echo '</td>';
	echo '</tr>';
}

/* end table */
echo '</table>';

?>
